<template>
  <div class="pageBox">
    <div class="top">
      <div class="topHead flex-center">
        <img src="@/image/my/backWhite.png" alt="" @click="$router.go(-1)" />
        <p>订单详情</p>
      </div>
      <div class="statusBox">
        <p v-if="details.status == -1">交易关闭</p>
        <p v-if="details.status == 0">待付款</p>
        <p v-if="details.status == 1">待发货</p>
        <p v-if="details.status == 2">待收货</p>
        <p v-if="details.status == 3">已完成</p>
        <p v-if="details.status == 7">拼团中</p>
        <p v-if="details.status == 8">拼团失败</p>
        <!-- <span>剩余23小时59分钟自动关闭</span> -->
      </div>
    </div>

    <div class="addressBox flex-between" v-if="details.addressId">
      <div class="address flex-row">
        <img class="dizhi_icn" src="@/image/home/dizhi_icn.png" alt="" />
        <div class="addr">
          <div class="addrTop flex-row">
            <p>{{ details.addressId.name }}</p>
            <span>{{ details.addressId.mobile }}</span>
          </div>
          <div class="addrBom">{{ details.addressId.address }}</div>
        </div>
      </div>
     
    </div>

    <div class="goodsInfoBox">
      <div class="goodsInfo flex-between" v-for="item in details.orderGoods" :key="item.id">
        <img class="infoLeft" :src="item.thumb" alt="" />
        <div class="infoRight">
          <div class="infoTitle">{{ item.title }}</div>
          <div class="sku flex-row" v-if="item.specTitle">
            <div class="skuItem">规格：{{ item.specTitle }}</div>
          </div>
          <div class="price">￥{{ item.price }}</div>
          <div class="number">x {{ item.num }}</div>
        </div>
      </div>
      <div class="subtotal flex-between">
        <p>商品小计</p>
        <span>￥{{ details.price }}</span>
      </div>
      <div class="total flex-between">
        <p>运费</p>
        <span>￥{{ details.freight }}</span>
      </div>
      <div class="total flex-between">
        <p>合计</p>
        <span>￥{{ details.finalPrice }}</span>
      </div>
    </div>

    <div class="orderInfo">
      <div class="orderInfoItem">订单编号：{{ details.orderNo }}</div>
      <div class="orderInfoItem">创建时间：{{ details.createdAt }}</div>
    </div>

    <div class="bom flex-row">
      <button class="closeBtn" @click.stop="onCloseBtn" v-if="details.status == 0">取消订单</button>
      <button class="payBtn" @click.stop="onPayBtn" v-if="details.status == 0">付款</button>
      <button class="closeBtn" @click.stop="goLogistics" v-if="details.status == 2">查看物流</button>
      <button class="payBtn" v-if="details.status == 2" @click.stop="onFinish">确认收货</button>
    </div>

    <van-popup v-model:show="show" position="bottom" round>
      <div class="popupBox">
        <img
          class="popupClose"
          src="@/image/my/popupClose.png"
          alt=""
          @click="show = false"
        />
        <div class="title flex-center">请选择原因</div>
        <div class="tips flex-center">
          订单一旦取消，无法恢复，金金豆将原路返还
        </div>
        <div class="reason">
          <div
            class="reasonItem flex-between"
            v-for="item in closeReason"
            :key="item.id"
            @click="onReasonItem(item)"
          >
            <div
              :class="[
                item.status ? 'reasonItemRActive reasonItemR' : 'reasonItemR',
              ]"
            >
              {{ item.title }}
            </div>
            <img :src="item.chooseImg" alt="" v-if="!item.status" />
            <img :src="item.choosedImg" alt="" v-else />
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script type="text/ecmascript-6">
import { Toast } from "vant";
import { defineComponent, ref } from "vue";
import router from "../../router";
import { post } from "../../untils/request";

export default defineComponent({
  setup() {
    const id = ref(
      router.currentRoute.value.query.id
        ? router.currentRoute.value.query.id
        : ""
    );
    const details = ref({});
    const show = ref(false);
    const reasonId = ref(1);
    const reasonTitle = ref("收货地址填错了");
    const closeReason = ref([
      {
        id: 1,
        title: "收货地址填错了",
        status: true,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 2,
        title: "忘记支付密码/余额不足",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 3,
        title: "无法正常支付",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 4,
        title: "不想买了",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 5,
        title: "其他原因",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
    ]);

    // 取消订单
    function onCloseBtn() {
      show.value = true;
    }

    function onReasonItem(param) {
      closeReason.value.forEach((item) => {
        item.status = false;
        if (item.id == param.id) {
          item.status = true;
          reasonId.value = item.id;
          reasonTitle.value = item.title;
          close();
        }
      });
    }

    function close() {
      post("/portal/Shop", {
        loading: true,
        module: "Order",
        interface: "1004",
        data: {
          id: details.value.id,
          reason: reasonTitle.value,
        },
      }).then(() => {
        Toast("取消成功");
        show.value = false;
        setTimeout(() =>{
          router.go(-1);
        }, 1000)
      });
    }

    function goLogistics() {
      router.push({
        path: "/logistics",
        query: {
          param: JSON.stringify(details.value),
        }
      });
    }

    function getDetails() {
      post("/portal/Shop", {
        module: "Order",
        interface: "1001",
        data: {
          id: id.value,
        },
      }).then((res) => {
        console.log("商品详情", res);
        details.value = res.data;
      });
    }
    getDetails();

     function onPayBtn() {
      console.log("付款");
      orderPay(details.value.orderNo)
    }

    //订单支付 余额支付
    function orderPay(orderNo) {
      post("/portal", {
        loading: true,
        module: "Pay",
        interface: "1000",
        data: {
          order_no: orderNo,
        },
      }).then((res) => {
        console.log("订单支付", res);
        router.replace("/paySuccess");
      });
    }

    // 确认收货
    function onFinish() {
       post("/portal/Shop", {
        loading: true,
        module: "Order",
        interface: "1003",
        data: {
          id: details.value.id,
        },
      }).then(() => {
        Toast("确认收货成功");
        setTimeout(() =>{
          router.go(-1);
        }, 1000)
      });
    }

    return {
      show,
      reasonId,
      reasonTitle,
      closeReason,
      id,
      details,
      onCloseBtn,
      onReasonItem,
      goLogistics,
      getDetails,
      onPayBtn,
      orderPay,
      onFinish,
    };
  },
});
</script>

<style scoped lang="less">
.pageBox {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  .top {
    height: 350px;
    background: linear-gradient(180deg, #b30b11 0%, #f50811 100%);
    .topHead {
      position: relative;
      height: 88px;
      img {
        width: 40px;
        height: 44px;
        position: absolute;
        left: 30px;
        top: 22px;
      }
      p {
        font-size: 36px;
        font-weight: 500;
        color: #ffffff;
      }
    }
    .statusBox {
      margin-top: 46px;
      padding-left: 30px;
      p {
        font-size: 40px;
        font-weight: 500;
        color: #ffffff;
        margin-bottom: 20px;
      }
      span {
        font-size: 26px;
        font-weight: 400;
        color: #ffffff;
      }
    }
  }
  .addressBox {
    padding: 50px 30px;
    background-color: #fff;
    .address {
      .dizhi_icn {
        width: 61px;
        height: 73px;
      }
      .addr {
        padding-left: 12px;
        .addrTop {
          p {
            font-size: 36px;
            font-weight: 500;
            color: #2e3033;
            margin-right: 20px;
          }
          span {
            font-size: 30px;
            font-weight: 400;
            color: #999999;
          }
        }
        .addrBom {
          font-size: 26px;
          font-weight: 400;
          color: #999999;
          margin-top: 10px;
        }
      }
    }
   
  }

  .goodsInfoBox {
    background-color: #ffffff;
    padding: 40px 30px;
    margin-top: 20px;

    .goodsInfo {
      width: 100%;
      margin-bottom: 20px;
      .infoLeft {
        width: 168px;
        height: 168px;
        border-radius: 10px;
      }
      .infoRight {
        width: 100%;
        padding-left: 20px;
        .infoTitle {
          font-size: 28px;
          font-weight: 500;
          color: #17181a;
        }
        .sku {
          flex-wrap: wrap;
          margin-top: 20px;
          .skuItem {
            font-size: 26px;
            font-weight: 400;
            color: #999999;
            margin-right: 14px;
          }
        }
        .price {
          text-align: right;
          font-size: 26px;
          font-weight: 500;
          color: #333333;
        }
        .number {
          text-align: right;
          font-size: 26px;
          font-weight: 400;
          color: #999999;
        }
      }
    }
    .subtotal {
      margin-top: 30px;
      p {
        font-size: 26px;
        font-weight: 400;
        color: #666666;
      }
      span {
        font-size: 26px;
        font-weight: 400;
        color: #17181a;
      }
    }
    .total {
      margin-top: 30px;
      p {
        font-size: 26px;
        font-weight: 400;
        color: #666666;
      }
      span {
        font-size: 26px;
        font-weight: 400;
        color: #e00b12;
      }
    }
  }
  .orderInfo {
    margin-top: 20px;
    padding: 40px 30px;
    background-color: #ffffff;
    .orderInfoItem {
      font-size: 26px;
      font-weight: 400;
      color: #666666;
      margin-bottom: 30px;
    }
    .orderInfoItem:nth-last-of-type(1) {
      margin-bottom: 0;
    }
  }
  .bom {
    width: 100%;
    height: 100px;
    background: #ffffff;
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 1;
    padding: 0 30px;
    button {
      width: 144px;
      height: 56px;
      border-radius: 10px;
      font-size: 24px;
      font-weight: 400;
      margin-left: 39px;
    }
    .closeBtn {
      color: #999999;
      background: #ffffff;
      border: 1px solid #cccccc;
    }
    .payBtn {
      color: #ffffff;
      background: linear-gradient(270deg, #b30b11 0%, #f50811 100%);
      border: none;
    }
    
  }
  .popupBox {
    padding: 46px 39px 34px 40px;
    position: relative;
    .popupClose {
      width: 48px;
      height: 48px;
      position: absolute;
      right: 19px;
      top: 19px;
    }
    .title {
      font-size: 36px;
      font-weight: 500;
      color: #333333;
    }
    .tips {
      font-size: 26px;
      font-weight: 400;
      color: #e00b12;
      margin-top: 16px;
      margin-bottom: 7px;
    }
    .reason {
      .reasonItem {
        margin-top: 72px;
        .reasonItemR {
          font-size: 30px;
          font-weight: 400;
          color: #333333;
        }
        .reasonItemRActive {
          font-weight: bold;
        }
        img {
          width: 40px;
          height: 40px;
        }
      }
    }
  }
}
</style>